package thread;

public class ThreadTerminated {
    public static void main(String[] args) throws InterruptedException {
        Thread thread=new Thread(()->{
            Thread currentThread=Thread.currentThread();//获取线程的引用
            while(!currentThread.isInterrupted()){
                System.out.println("hello thread");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
//                    throw new RuntimeException(e);
                    System.out.println("执行到catch操作");
                    return;
                }
            }
        });
        thread.start();
        Thread.sleep(3000);
        //在主线程中设置isInterrupted标志位false->true,控制thread线程被终止
        thread.interrupt();
    }



    private static boolean isQuit=false;
    public static void main1(String[] args) throws InterruptedException {
        Thread thread=new Thread(()->{
            while(!isQuit){
                System.out.println("hello thread");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.println("thread线程执行结束");
        });
        thread.start();
        Thread.sleep(2000);
        //通过修改isQuit变量,就能影响thread线程的结束了
        System.out.println("main线程尝试终止thread线程");
        isQuit=true;
    }
}
